package net.kucoe.macarun.sample;

import static net.kucoe.macarun.Macarun.match;
import net.kucoe.macarun.*;

/**
 * {@link Macarun} sample.
 * 
 * @author Vitaliy Basyuk
 */
public class MacarunEasySample {
    
    /**
     * Main method
     * 
     * @param args
     */
    public static void main(final String[] args) {
        MacarunEasySample sample = new MacarunEasySample();
        sample.doMatch("1");
        sample.doMatch(null);
        sample.doMatch("aaa");
        sample.doMatch(12);
        sample.doMatch(12.9);
    }
    
    private void doMatch(final Object object) {
        Match match = match(object);
        match.inCase(String.class).check(new Check<String>() {
            public boolean check(final String object) {
                try {
                    Integer.parseInt(object);
                } catch (NumberFormatException e) {
                    return false;
                }
                return true;
            }
        }).run(new Run<String, Integer>() {
            public Integer run(final String object) {
                return Integer.valueOf(object);
            }
        }).breaking();
        match.inCase(Number.class).run(new Run<Number, Integer>() {
            public Integer run(final Number object) {
                return object.intValue();
            }
        }).breaking();
        Object result = match.run();
        if (result != null) {
            System.out.println(result);
        }
    }
    
}
